'use strict';

/**
 * @ngdoc function
 * @name mytodoApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the mytodoApp
 */
angular.module('mytodoApp')
    .controller('MainCtrl', function ($scope, localStorageService) {
        //$scope.todos = [
        //    'Todo 01',
        //    'Todo 02',
        //    'Todo 03'
        //];

        // for test
        //$scope.todos = [];

        // 使用浏览器本地存储
        var todosInStore = localStorageService.get("todos");
        $scope.todos = todosInStore && todosInStore.split('\n') || [];
        $scope.$watch('todos', function () {
            localStorageService.add('todos', $scope.todos.join('\n'));
        }, true);

        // 添加todo
        $scope.addTodo = function () {
            $scope.todos.push($scope.todo);
            $scope.todo = '';
        };
        // 移除todo
        $scope.removeTodo = function (index) {
            $scope.todos.splice(index, 1);
        };
    });
